package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 11/23/12
 * Time: 9:59 AM
 * To change this template use File | Settings | File Templates.
 **/
@Namespace("/Protocols/SiteEquipmentSelection")
public class SiteEquipmentSelectionController extends ActionSupport implements ModelDriven {

    private List<Centro> sites;
    private ProtocoloCentroService protocoloCentroService;
    private ProtocolCRUDService protocolCRUDService;
    private Centro site;
    private ProtocoloCompromisopais protocoloCompromisopais;
    private Equipmentcertification equipmentcertification;
    private EquipmentcertificationService equipmentcertificationService;
    private List<EquipmentSiteProtocoloCompromisoPais> list;
    private EquipmentSiteProtocoloCompromisoPaisService equipmentSiteProtocoloCompromisoPaisService;
    private CentroCRUDService centroCRUDService;

    @Autowired
    public SiteEquipmentSelectionController(
            ProtocoloCentroService protocoloCentroService,
            ProtocolCRUDService protocolCRUDService,
            EquipmentcertificationService equipmentcertificationService,
            EquipmentSiteProtocoloCompromisoPaisService equipmentSiteProtocoloCompromisoPaisService, CentroCRUDService centroCRUDService) {
        this.protocoloCentroService = protocoloCentroService;
        this.protocolCRUDService = protocolCRUDService;
        this.equipmentcertificationService = equipmentcertificationService;
        this.equipmentSiteProtocoloCompromisoPaisService = equipmentSiteProtocoloCompromisoPaisService;
        this.centroCRUDService = centroCRUDService;
    }


    @Action(value="main", results={
            @Result(name="success", location="/protocols.siteEquipmentSelection.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
        sites = protocoloCentroService.getSitesByProtocoloCompromisoPaisAndIPCs(
                protocoloCompromisopais,
                new ArrayList<InvestigadorProtocoloCentro>());

        if(equipmentcertification != null){
            Centro site = centroCRUDService.findById(equipmentcertification.getCentro().getId());
            list = equipmentSiteProtocoloCompromisoPaisService.getList(site, protocoloCompromisopais);
        }

        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String saveEquipment() throws NoProtocoloCompromisopaisSelectedException {
        protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
        Centro site = centroCRUDService.findById(equipmentcertification.getCentro().getId());
        equipmentSiteProtocoloCompromisoPaisService.save(list,protocoloCompromisopais,site);
        return SUCCESS;
    }

    @Override
    public List<EquipmentSiteProtocoloCompromisoPais> getModel() {
        return list;
    }

    public List<Centro> getSites() {
        return sites;
    }

    public void setSites(List<Centro> sites) {
        this.sites = sites;
    }

    public ProtocoloCompromisopais getProtocoloCompromisopais() {
        return protocoloCompromisopais;
    }

    public void setProtocoloCompromisopais(ProtocoloCompromisopais protocoloCompromisopais) {
        this.protocoloCompromisopais = protocoloCompromisopais;
    }

    public Equipmentcertification getEquipmentcertification() {
        return equipmentcertification;
    }

    public void setEquipmentcertification(Equipmentcertification equipmentcertification) {
        this.equipmentcertification = equipmentcertification;
    }

    public List<EquipmentSiteProtocoloCompromisoPais> getList() {
        return list;
    }

    public void setList(List<EquipmentSiteProtocoloCompromisoPais> list) {
        this.list = list;
    }


}
